package sg.edu.nus.iss.cats.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.*;

import sg.edu.nus.iss.cats.model.*;
import sg.edu.nus.iss.cats.controller.*;
import sg.edu.nus.iss.cats.dataaccess.*;

public class CourseViewDetailsAction extends Action {

    public ActionForward execute(ActionMapping mapping,
                                 ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response) {

        HttpSession session = request.getSession();

        try {
            System.out.println("In CourseViewDetailsAction");

            UserSession userSession =
                (UserSession) session.getAttribute("USERSESSION");
            Employee employee = userSession.getEmployee();

            System.out.println(
                "CourseViewDetailsAction" + employee.getEmployeeId());

            String courseId = request.getParameter("course_id");
            CourseDAO courseDAO = DAOFactory.getInstance().getCourseDAO();
            Course course = courseDAO.findCourse(courseId);
            
            System.out.println("CourseViewDetailsAction : course events" + course.getCourseEvents().length);
            request.setAttribute("course", course);

            // If the application is pending approval, it can be modified
            // (by the owner) or approved (by the manager)
            boolean updateCourse = false;
            boolean approveCourse = false;

            String status = course.getStatus();     
            
             System.out.println("CourseViewDetailsAction : status " + course.getStatus());
            
            if (status.equals(Course.SUBMITTED) || status.equals(Course.UPDATED)) {

                // If this is the owner, the modification box will be shown
                String ownerId = course.getEmployeeId();
                if (ownerId.equals(employee.getEmployeeId())) {
                    updateCourse = true;
                } else {

                    // If this is the manager, the approval box will be shown
                    EmployeeDAO empDAO =
                        DAOFactory.getInstance().getEmployeeDAO();
                    Employee owner = empDAO.findEmployee(ownerId);
                    String managerId = owner.getManagerId();
                    if (managerId.equals(employee.getEmployeeId())) {
                        approveCourse = true;
                    }
                }
            }
            request.setAttribute("updateCourse", new Boolean(updateCourse));
            request.setAttribute("approveCourse", new Boolean(approveCourse));

            System.out.println("CourseViewDetailsAction success");
            return mapping.findForward("success");

        } catch (Exception e) {
            //return an ActionForward respresenting the logical failure page
            ActionErrors errors = new ActionErrors();
            errors.add(
                ActionErrors.GLOBAL_ERROR,
                new ActionError("error.password.mismatch"));
            // Report any errors we have discovered back to the original form
            saveErrors(request, errors);
            servlet.log(
                "CourseViewDetailsAction: Employee could not log on in session ",
                0);
            return mapping.findForward("error");
        }
    }
}